#!/bin/bash

workspace="/opt/check"
# 先更新代码
git pull origin master
cd $workspace/app
git pull origin master

#激活一下python环境 
source /root/venv/bin/activate

function rand(){
  min=$1
  max=$(($2-$min+1))
  num=$(($RANDOM+1000000000)) #增加一个10位的数再求余
  echo $(($num%$max+$min))
}

ProductType=`dmidecode |grep "Product" |sort -n |uniq |head |awk '{print $3}'`
dev_serial_number=`dmidecode -t system | grep 'Serial Number' |sort -n |uniq |head |awk '{print $3}'`

data_path=$workspace"/data"
if [ ! -d $report_path ];then
  mkdir -p $report_path
fi

data_path=$data_path"/"$dev_serial_number
if [ ! -d $data_path ];then
  mkdir -p $data_path
else
  rnd=$(rand 400000 500000)
  data_path=$data_path'#'$rnd
  mkdir -p $data_path
fi



export data_path=$data_path
export workspace=$workspace
export dev_serial_number=`echo $data_path|awk -F '/' '{print $NF}'` 

bmc_OriginIpAddress=`ipmitool lan print |grep 'IP Address' |egrep '[0-9]' |awk '{print $4}'`
#修改IPMI的IP
bmc_ip='172.21.1.'$(expr `date "+%H-%M-%S" |awk -F '-' '{print $1$2$3$4}' ` % 252 + 1)
ipmitool lan set 1 ipaddr $bmc_ip
#启用板载网卡
ip addr |grep mq|awk '{print $2}'|awk -F ':' '{print $1}' |xargs ifup

sleep 3
_now=`date "+%Y-%m-%d %H:%M:%S"` 
echo -e "服务器状态检查 开始\t\t\t$_now"   >>$data_path'/readme.log'
echo -e "数据目录$data_path"              >>$data_path'/readme.log'
echo -e '机器基本信息' $ProductType       >>$data_path'/readme.log'
echo -e '     型号:'  $ProductType       >>$data_path'/readme.log'
echo -e '   序列号:'  $dev_serial_number >>$data_path'/readme.log'
echo -e '最新BMCIP:' $bmc_ip             >>$data_path'/readme.log'
sh $workspace'/app/starter.sh'           >>$data_path'/readme.log'
echo -e "服务器状态检查 完成\t\t\t$_now">>$data_path'/readme.log'
echo '程序运行结束'                       >>$data_path'/readme.log' 